//
//  GoodsImageCell.swift
//  Box
//
//  Created by mn on 2025/4/2.
//
import UIKit
class GoodsImageCell: UITableViewCell {
    
    let imageView1 = UIImageView()
    var imageLoadedCallback: (() -> Void)? // 用于通知外部刷新高度
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        selectionStyle = .none
        imageView1.contentMode = .scaleAspectFill
        imageView1.clipsToBounds = true
        contentView.addSubview(imageView1)
        imageView1.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(200).priority(.low) // 初始高度
        }
    }
    
    func setImage(url: String) {
        guard let imageURL = URL(string: url) else { return }
        imageView1.kf.setImage(with: imageURL, completionHandler: { [weak self] result in
            guard let self = self else { return }
            switch result {
            case .success(let value):
                let image = value.image
                let width = UIScreen.main.bounds.width
                let height = image.size.height / image.size.width * width
                self.imageView1.snp.remakeConstraints { make in
                    make.edges.equalToSuperview()
                    make.height.equalTo(height).priority(.low)
                }
                self.imageLoadedCallback?()
            case .failure:
                break
            }
        })
    }
}
class GoodsTextCell: UITableViewCell{
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 17)
        label.textColor = .init(hex: "#292D32")
        label.textAlignment = .center
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        selectionStyle = .none
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
        }
    }
    
}
